#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

#include "SensorCom.h"


int main()
{
    FrameAPI myApiFrame;

    printf("<?>DEBUG - Device initializing\n");
    initSensor();
    printf("<?>DEBUG - Device registering\n");
    if(registerDevice())
    {
        printf("<?>DEBUG - Device registered\n");
        while(1)
        {
            // DEVICE WAKE UP
            printf("<?>DEBUG - Device waking up\n");

            sendSensorData();
            printf("<?>DEBUG - Sending data\n");
            while(getTimeToSleep() == 0)
            {
                if(recept(&myApiFrame, 5) != 0)
                {
                    printf("<?>DEBUG - New packet received, handle\n");
                    handleDataPacket(myApiFrame);
                }
            }
            // DEVICE IN SLEEP MODE
            printf("<?>DEBUG - Device in sleep mode\n");
            setWatchDog(getTimeToSleep());
            while(checkWatchDog() ==  FAUX);
            setTimeToSleep(0);
        }
    }
    printf("<!>ERROR - Failed to register\n");
    getchar();
    fermeCom();
    return EXIT_SUCCESS;
}
